Skip to content

Commit

Permalink
[feat] sdk支持异步初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
huangriya committed Sep 18, 2023
1 parent 2a36f2f commit ee7a033
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ANS=WX
LIB=WeChat
LibVERSION=5.0.3
LibVERSION=5.0.4
6 changes: 3 additions & 3 deletions demo/demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ AnalysysAgent.encrypt = AnalysysEncryption
import AnalysysAgent from './sdk/AnalysysAgent_WX_SDK.es6.min.js';

AnalysysAgent.registerSuperProperty('sfsf', 1)


AnalysysAgent.identify('openid123123')
AnalysysAgent.init({
appkey: '47fce41a0472c616',
uploadURL: 'https://uba-up.analysysdata.com',
Expand All @@ -26,13 +29,10 @@ AnalysysAgent.onReady = function(config) {

AnalysysAgent.onBeforeStartUp = function (res) {
console.log('开始发送预制启动事件')
console.log(res)
}

AnalysysAgent.onAfterStartUp = function (res) {
console.log('预制启动事件发送成功')
console.log(res)
console.timeEnd('time')
}

App({
Expand Down
2 changes: 1 addition & 1 deletion demo/demo/sdk/AnalysysAgent_WX_SDK.es6.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/demo/sdk/AnalysysAgent_WX_SDK.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions demo/demoPlugin/sdk/AnalysysAgent_WX_SDK.plugin.es6.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions demo/demoPlugin/sdk/AnalysysAgent_WX_SDK.plugin.min.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions demo/taro/sdk/AnalysysAgent_WX_SDK.custom.es6.min.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions demo/taro/sdk/AnalysysAgent_WX_SDK.custom.min.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions dist/AnalysysAgent_WX_SDK.custom.es6.min.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions dist/AnalysysAgent_WX_SDK.custom.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/AnalysysAgent_WX_SDK.es6.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/AnalysysAgent_WX_SDK.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/AnalysysAgent_WX_SDK.plugin.es6.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/AnalysysAgent_WX_SDK.plugin.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asd-wechat-sdk",
"version": "5.0.3",
"version": "5.0.4",
"description": "This is the offical wechat SDK for Analysys.",
"main": "dist/AnalysysAgent_WX_SDK.min.js",
"module": "dist/AnalysysAgent_WX_SDK.es6.min.js",
Expand Down
75 changes: 36 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

import { initConfig } from './types'
import { setConfig, config } from './store/config'
import { globalWindow, optionsDefault } from './constant/index'
import { coreInit } from './store/core'
import ready from './module/ready'
import { successLog } from './module/printLog/index'
import { globalWindow, optionsDefault, $lib_version } from './constant/index'
import ready, { implementAallbackArr } from './module/ready'
import { successLog, errorMessage } from './module/printLog/index'
import {
pageView,
profileSetOnce, profileSet, profileAppend, profileIncrement, profileDelete, profileUnset,
Expand All @@ -25,64 +24,62 @@ import {
pageProperty
} from './module/methods/index'
import autoTrigger from './module/autoTrigger'
import { setNetwork } from './store/network'
import { getServerTime } from './store/time'


class ArkWxSdk {
constructor () {
coreInit()
Promise.all([setNetwork(), getServerTime()]).then(() => {
implementAallbackArr()
})
}
version: string = $lib_version;
config: initConfig = config;
pageView = ready(pageView);
share = share;
registerSuperProperty = ready(registerSuperProperty);
registerSuperProperties = ready(registerSuperProperties);
getSuperProperty = getSuperProperty;
getSuperProperties = getSuperProperties;
unRegisterSuperProperty = unRegisterSuperProperty;
clearSuperProperties = clearSuperProperties;
profileSetOnce = profileSetOnce;
profileSet = profileSet;
profileAppend = profileAppend;
profileIncrement = profileIncrement;
profileDelete = profileDelete;
profileUnset = profileUnset;
reset = reset;
share = ready(share);
registerSuperProperty = ready(registerSuperProperty, true);
registerSuperProperties = ready(registerSuperProperties, true);
getSuperProperty = ready(getSuperProperty);
getSuperProperties = ready(getSuperProperties);
unRegisterSuperProperty = ready(unRegisterSuperProperty);
clearSuperProperties = ready(clearSuperProperties);
profileSetOnce = ready(profileSetOnce);
profileSet = ready(profileSet);
profileAppend = ready(profileAppend);
profileIncrement = ready(profileIncrement);
profileDelete = ready(profileDelete);
profileUnset = ready(profileUnset);
reset = ready(reset);
track = ready(track);
timeEvent = timeEvent;
alias = ready(alias);
getPresetProperties = getPresetProperties;
identify = identify;
getDistinctId = getDistinctId;
pageProperty = pageProperty;
getPresetProperties = ready(getPresetProperties);
identify = ready(identify, true);
getDistinctId = ready(getDistinctId);
pageProperty = ready(pageProperty);

// 初始化传入配置
init (config: initConfig) {
if (!config.appkey) throw new Error(errorMessage['60006'])
if (!config.uploadURL) throw new Error(errorMessage['60007'])
setConfig(config).then(o => {
this.config = o
successLog({
code: 20007
})
implementAallbackArr()
})
}
}

// 初始化传入配置
Object.keys(optionsDefault()).forEach(o => {
Object.defineProperty(ArkWxSdk.prototype, o, {
get: function get() {
return this.config[o];
},
set: function set(value) {
const _this = this;
setConfig({
[o]: value
}).then(function (o) {
_this.config = o;
})
},
enumerable: false,
configurable: true
})
Object.defineProperty(ArkWxSdk.prototype, 'appKey', {
set: function set() {
throw new Error('请使用init方法初始化sdk')
},
enumerable: false,
configurable: true
})

const ArkSdk = new ArkWxSdk()
Expand Down
4 changes: 2 additions & 2 deletions src/module/autoTrigger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export function AppFn (app) {
})

// 自动上报启动事件
appFnApply(app, 'onShow', () => {
appFnApply(app, 'onShow', (...arg) => {
if (!eventAttribute.startup.state) {
ready(startUp)()
ready(startUp)(...arg)
eventAttribute.startup.state = true
}
})
Expand Down
6 changes: 4 additions & 2 deletions src/module/methods/identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export function identify(distinctId: string) : void {
* @returns
*/

export function getDistinctId() : string {
return getAnonymousID()
export function getDistinctId(callback?: (id: string) => void) : string {
const id = getAnonymousID()
callback && callback(id)
return id
}
2 changes: 1 addition & 1 deletion src/module/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { registerSuperProperty, registerSuperProperties, getSuperProperty, getSuperProperties, unRegisterSuperProperty, clearSuperProperties} from './superProperty'
import pageView from './pageView'
import pageClose from './pageClose'
import pageClose from './pageclose'
import startUp from './startUp'
import appStart from './appStart'
import end from './end'
Expand Down
2 changes: 1 addition & 1 deletion src/module/printLog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function lenRrrlog (lens) {
return `{FN}:The length of the property key (string[{KEY}]) needs to be ${lens} !`
}

const errorMessage = {
export const errorMessage = {
'common': '',
'60001': '{FN}:Property key invalid, support type: String \n' +
'current key:{KEY}\n' +
Expand Down
112 changes: 79 additions & 33 deletions src/module/ready/index.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,104 @@
import { setNetwork, isGetNetWork } from '../../store/network'
import { getServerTime, isGetServerTime } from '../../store/time'
import { core, resetCore, setCoreParams } from '../../store/core'
import { config } from '../../store/config'
import { config, isInitConfig } from '../../store/config'
import lifecycleHook from '../../store/lifecycleHook'
import { getNow } from '../../store/time'

/**
* 准备就绪后开始上报数据
*/

interface callbackArrType {
fn: (args: any[]) => void;
arg: any;
xwhen?: number;
}

const callbackArr: callbackArrType[] = []
let callbackArr: callbackArrType[] = []
let callbackfirstArr: callbackArrType[] = []

let isReady = false

function ready (callback) {
return function(...args: any[]) {

// 没有获取过NetWork和ServerTime之前先把触发事件存起来,等NetWork和ServerTime获取过后再调用
if (!isGetNetWork || !isGetServerTime) {
callbackArr.push({
fn: callback,
arg: args
// sdk是否准备就绪
export const isReady = () => isGetServerTime && isInitConfig && isGetNetWork

// 执行缓存函数
export function implementAallbackArr () {


if (isReady()) {

// onReady函数,sdk生命周期钩子,sdk初次初始化完成(采集数据之前的准备工作已到位)
lifecycleHook.onReady(config)

// 执行sdk没有初始化完成之前缓存函数
if (callbackfirstArr && callbackArr.length) {
callbackfirstArr.forEach(o => {
return o.fn.apply(o.fn, o.arg)
})
callbackfirstArr = []
}

if (!isReady) {
Promise.all([setNetwork(), getServerTime()]).then(function() {
const ARKDEBUG = core.ARKDEBUG
if (config.appkey !== core.ARKAPPID || (ARKDEBUG === 1 && ARKDEBUG !== config.debugMode) || core.ARKUPLOADURL !== config.uploadURL) {
if (core.ARKAPPID) {
resetCore()
}
setCoreParams({
ARKAPPID: config.appkey,
ARKDEBUG: config.debugMode,
ARKUPLOADURL: config.uploadURL
})
}

// onReady函数,sdk生命周期钩子,sdk初次初始化完成(采集数据之前的准备工作已到位)
lifecycleHook.onReady(config)

callbackArr.forEach(o => {
o.fn.apply(o.fn, o.arg)
})
})
isReady = true
if (callbackArr && callbackArr.length) {
callbackArr.forEach(o => {
return o.fn.apply(o.fn, o.arg)
})
callbackArr = []
}
}
}

function ready (callback, isTop?: boolean) {
return function(...args: any[]) {
if (!isReady()) {
const obj = {
fn: callback,
arg: args,
xwhen: getNow()
}
isTop ? callbackfirstArr.push(obj) : callbackArr.push(obj)
} else {
return callback.apply(callback, args)
return callback.apply(callback, args)
}
}
}

// function ready (callback) {
// return function(...args: any[]) {

// // 没有获取过NetWork和ServerTime之前先把触发事件存起来,等NetWork和ServerTime获取过后再调用
// if (!isGetNetWork || !isGetServerTime) {
// callbackArr.push({
// fn: callback,
// arg: args
// })

// if (!isReady) {
// Promise.all([setNetwork(), getServerTime()]).then(function() {
// const ARKDEBUG = core.ARKDEBUG
// if (config.appkey !== core.ARKAPPID || (ARKDEBUG === 1 && ARKDEBUG !== config.debugMode) || core.ARKUPLOADURL !== config.uploadURL) {
// if (core.ARKAPPID) {
// resetCore()
// }
// setCoreParams({
// ARKAPPID: config.appkey,
// ARKDEBUG: config.debugMode,
// ARKUPLOADURL: config.uploadURL
// })
// }

// // onReady函数,sdk生命周期钩子,sdk初次初始化完成(采集数据之前的准备工作已到位)
// lifecycleHook.onReady(config)

// implementAallbackArr()
// })
// isReady = true
// }
// } else {
// return callback.apply(callback, args)
// }
// }
// }

export default ready
6 changes: 6 additions & 0 deletions src/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { optionsDefault } from '../constant'
import { isBoolean, isNumber, isString } from '../utils/type'
import { lengthCheck } from '../utils/verify'
import { errorLog } from '../module/printLog'
import { coreInit } from './core'

const configRule = {
appkey: {
Expand Down Expand Up @@ -58,6 +59,9 @@ const configRule = {
}
}

// 是否完成初始化参数配置
export let isInitConfig = false

export const config : initConfig = optionsDefault()

export function setConfig (options: initConfig, isVerify = true) : Promise<object> {
Expand Down Expand Up @@ -88,6 +92,8 @@ export function setConfig (options: initConfig, isVerify = true) : Promise<objec
}
})
}
coreInit()
isInitConfig = true
resolve(config)
})
}
Expand Down
Loading

0 comments on commit ee7a033

Please sign in to comment.