Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ready called #390

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/demo/src/utils/analytics/example-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const prefixEventNames = {
/* enrich telemetry payload */
const enrichTelemetryPayload = {
NAMESPACE: 'enrich',
enabled: false,
initialize: () => {
console.log('enrich INIT!!!!')
window.HIIIIII = true
},
trackStart: ({ payload, instance }) => {
return {
...payload,
Expand Down
3 changes: 2 additions & 1 deletion examples/demo/src/utils/analytics/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// import analytics from './basic'
import analytics from './just-ga'
// import analytics from './just-ga'
import analytics from './example-1'

// import analytics from './example-multiple-gtm-instances'

Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-core/src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const coreEvents = [
/*
* `loadPlugin` - Fires when 'analytics.loadPlugin()' is called
*/
// 'loadPlugin',
'loadPlugin',
/******************
* Browser activity events
******************/
Expand Down
30 changes: 23 additions & 7 deletions packages/analytics-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,19 @@ function analytics(config = {}) {
const allSystemEvents = Array.from(uniqueEvents).sort()

/* plugin methods(functions) must be kept out of state. thus they live here */
const getPlugins = () => customPlugins
const getPlugins = (selectedPlugins, asArray) => {
let pluginObj = customPlugins
if (selectedPlugins) {
const foundPlugins = ensureArray(selectedPlugins)
pluginObj = Object.keys(customPlugins).filter((name) => {
return foundPlugins.includes(name)
}).reduce((acc, curr) => {
acc[curr] = customPlugins[curr]
return acc
}, {})
}
return !asArray ? pluginObj : Object.keys(pluginObj).map((name) => customPlugins[name])
}

const {
addMiddleware,
Expand Down Expand Up @@ -176,6 +188,8 @@ function analytics(config = {}) {
visitorInfo.anonymousId = uuid()
}

let readyCalled = false

/**
* Async Management methods for plugins.
*
Expand Down Expand Up @@ -249,15 +263,16 @@ function analytics(config = {}) {
*
* @example
* analytics.plugins.load('segment')
@TODO implement
* analytics.plugins.load(['segment', 'google-analytics'])
*/
load: (plugins) => {
console.log('plugins', plugins)
store.dispatch({
type: EVENTS.loadPlugin,
// Todo handle multiple plugins via array
plugins: (plugins) ? [plugins] : Object.keys(getPlugins()),
plugins: (plugins) ? ensureArray(plugins) : Object.keys(getPlugins()),
readyCalled,
})
},
*/
/* @TODO if it stays, state loaded needs to be set. Re PLUGIN_INIT above
add: (newPlugin) => {
if (typeof newPlugin !== 'object') return false
Expand All @@ -276,7 +291,6 @@ function analytics(config = {}) {
...parsedOptions.methods
}

let readyCalled = false
/**
* Analytic instance returned from initialization
* @typedef {Object} AnalyticsInstance
Expand Down Expand Up @@ -550,8 +564,8 @@ function analytics(config = {}) {
// If ready already fired. Call callback immediately
if (readyCalled) callback({ plugins, instance })
return instance.on(EVENTS.ready, (x) => {
callback(x)
readyCalled = true
callback(x)
})
},
/**
Expand Down Expand Up @@ -965,6 +979,8 @@ function analytics(config = {}) {

/* All plugins registered initialize, is last loop */
if (parsedOptions.pluginsArray.length === (i + 1)) {
console.log('enabledPlugins', enabledPlugins)
console.log('disabledPlugins', disabledPlugins)
store.dispatch({
type: EVENTS.initializeStart,
plugins: enabledPlugins,
Expand Down
1 change: 1 addition & 0 deletions packages/analytics-core/src/middleware/plugins/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default async function (action, getPlugins, instance, store, eventsInfo)

/* If analytics.plugin.enable calls do special behavior */
if (originalType === EVENTS.initializeStart && action.fromEnable) {
console.log('from enable action', action)
// Return list of all enabled plugins that have NOT been initialized yet
activePlugins = Object.keys(state.plugins).filter((name) => {
const info = state.plugins[name]
Expand Down
44 changes: 14 additions & 30 deletions packages/analytics-core/src/middleware/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import runPlugins from './engine'
export default function pluginMiddleware(instance, getPlugins, systemEvents) {
const isReady = {}
return store => next => async action => {
const { type, abort, plugins } = action
const { type, abort, readyCalled, plugins } = action
let updatedAction = action

if (abort) {
Expand All @@ -30,39 +30,27 @@ export default function pluginMiddleware(instance, getPlugins, systemEvents) {
setTimeout(() => runCallback(action.meta.rid, { payload: action }), 0)
}

/* @TODO implement
if (type === EVENTS.loadPlugin) {
// Rerun initialize calls in plugins
const allPlugins = getPlugins()
const pluginsToLoad = Object.keys(allPlugins).filter((name) => {
return plugins.includes(name)
}).reduce((acc, curr) => {
acc[curr] = allPlugins[curr]
return acc
}, {})
const pluginsToLoad = getPlugins(plugins)
const initializeAction = {
type: EVENTS.initializeStart,
plugins: plugins
plugins: plugins,
fromEnable: true,
}
console.log('initializeAction', initializeAction)
const updated = await runPlugins(initializeAction, pluginsToLoad, instance, store, systemEvents)
return next(updated)
}
*/

// || type.match(/^initializeAbort:/)
if (type === EVENTS.initializeEnd) {
const allPlugins = getPlugins()
const pluginsArray = Object.keys(allPlugins)
const allRegisteredPlugins = pluginsArray.filter((name) => {
return plugins.includes(name)
}).map((name) => {
return allPlugins[name]
})
const enabledPlugins = getPlugins(plugins, true)
console.log('enabledPlugins', enabledPlugins)
let completed = []
let failed = []
let disabled = action.disabled
// console.log('allRegisteredPlugins', allRegisteredPlugins)
const waitForPluginsToLoad = allRegisteredPlugins.map((plugin) => {
// console.log('allEnabledPlugins', allEnabledPlugins)
const waitForPluginsToLoad = enabledPlugins.map((plugin) => {
const { loaded, name, config } = plugin
const loadedFn = () => loaded({ config }) // @TODO add in more to api to match other funcs?
/* Plugins will abort trying to load after 10 seconds. 1e4 === 10000 MS */
Expand Down Expand Up @@ -96,17 +84,13 @@ export default function pluginMiddleware(instance, getPlugins, systemEvents) {

Promise.all(waitForPluginsToLoad).then((calls) => {
// setTimeout to ensure runs after 'page'
const payload = {
plugins: completed,
failed: failed,
disabled: disabled
}
setTimeout(() => {
if (pluginsArray.length === (waitForPluginsToLoad.length + disabled.length)) {
if (!readyCalled && enabledPlugins.length === waitForPluginsToLoad.length) {
store.dispatch({
...{ type: EVENTS.ready },
...payload,

type: EVENTS.ready,
plugins: completed,
failed: failed,
disabled: disabled
})
}
}, 0)
Expand Down
Empty file.