@jetthai/cuora 1.0.178 → 1.0.180

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.0.180](https://github.com/jetthai/cuora-module/compare/v1.0.179...v1.0.180) (2022-09-28)
6
+
7
+ ### [1.0.179](https://github.com/jetthai/cuora-module/compare/v1.0.178...v1.0.179) (2022-09-26)
8
+
5
9
  ### [1.0.178](https://github.com/jetthai/cuora-module/compare/v1.0.177...v1.0.178) (2022-09-26)
6
10
 
7
11
  ### [1.0.177](https://github.com/jetthai/cuora-module/compare/v1.0.168-1...v1.0.177) (2022-09-23)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.0.178",
3
+ "version": "1.0.180",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
@@ -0,0 +1,114 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import api from '../api/game.js'
3
+
4
+ export default function (request) {
5
+ return createMachine(
6
+ {
7
+ id: 'favorite',
8
+ type: 'parallel',
9
+ context: {
10
+ list: [],
11
+ },
12
+ states: {
13
+ fetching: {
14
+ invoke: {
15
+ src: 'fetchList',
16
+ onDone: {
17
+ actions: 'setList',
18
+ },
19
+ onError: {
20
+ actions: assign({ list: [] }),
21
+ },
22
+ },
23
+ },
24
+ adding: {
25
+ initial: 'disable',
26
+ states: {
27
+ disable: {},
28
+ enable: {
29
+ invoke: {
30
+ src: 'add',
31
+ onDone: {},
32
+ onError: {
33
+ actions: 'removeFromList',
34
+ },
35
+ },
36
+ },
37
+ },
38
+ },
39
+ removing: {
40
+ initial: 'disable',
41
+ states: {
42
+ disable: {},
43
+ enable: {
44
+ invoke: {
45
+ src: 'remove',
46
+ onDone: {},
47
+ onError: {
48
+ actions: 'addToList',
49
+ },
50
+ },
51
+ },
52
+ },
53
+ },
54
+ },
55
+ on: {
56
+ ADD: {
57
+ target: 'adding.enable',
58
+ actions: 'addToList',
59
+ cond: 'isNotExist',
60
+ },
61
+ REMOVE: {
62
+ target: 'removing.enable',
63
+ actions: 'removeFromList',
64
+ cond: 'isExist',
65
+ },
66
+ },
67
+ },
68
+ {
69
+ actions: {
70
+ setList: assign((context, event) => {
71
+ return {
72
+ list: event.data,
73
+ }
74
+ }),
75
+ addToList: assign((context, event) => {
76
+ return {
77
+ list: [...context.list, event.data],
78
+ }
79
+ }),
80
+ removeFromList: assign((context, event) => {
81
+ return {
82
+ list: context.list.filter((item) => item.gameCode !== event.data.gameCode),
83
+ }
84
+ }),
85
+ },
86
+ guards: {
87
+ isExist: (context, event) => context.list.some((item) => item.gameCode === event.data.gameCode),
88
+ isNotExist: (context, event) => !context.list.some((item) => item.gameCode === event.data.gameCode)
89
+ },
90
+ services: {
91
+ add: async (context, event) => {
92
+ try {
93
+ await request.send(api.addListFavorite(event.data.gameCode))
94
+ return event.data
95
+ } catch (error) {
96
+ throw event.data
97
+ }
98
+ },
99
+ remove: async (context, event) => {
100
+ try {
101
+ await request.send(api.removeListFavorite(event.data.gameCode))
102
+ return event.data
103
+ } catch (error) {
104
+ throw event.data
105
+ }
106
+ },
107
+ fetchList: async (context, event) => {
108
+ const { data } = await request.send(api.getListMyFavorite())
109
+ return data
110
+ },
111
+ },
112
+ }
113
+ )
114
+ }
@@ -1,7 +1,7 @@
1
1
  import { assign, createMachine } from 'xstate'
2
2
  import api from '../api/game.js'
3
3
  import UAParser from 'ua-parser-js'
4
- import { MY_FAVORITE } from '../util/constant.js'
4
+ import { GAME_SEARCH } from '../util/constant.js'
5
5
 
6
6
  export default function (request) {
7
7
  return createMachine(
@@ -17,7 +17,6 @@ export default function (request) {
17
17
  parentCategoryId: null,
18
18
  totalPage: 1,
19
19
  },
20
- favoriteList: [],
21
20
  // play params
22
21
  game: {
23
22
  providerId: null,
@@ -29,6 +28,7 @@ export default function (request) {
29
28
  lang: 'en_US',
30
29
  isMobile: new UAParser().getResult().device.type,
31
30
  gameURL: null,
31
+ ...oldContext
32
32
  },
33
33
  states: {
34
34
  flow: {
@@ -51,16 +51,6 @@ export default function (request) {
51
51
  target: 'connecting',
52
52
  actions: ['setGameInfo', 'setLang'],
53
53
  },
54
- ADD_FAVORITE: {
55
- target: 'adding',
56
- cond: 'isNotFavorite',
57
- actions: 'addToFavoriteList',
58
- },
59
- REMOVE_FAVORITE: {
60
- target: 'removing',
61
- cond: 'isFavorite',
62
- actions: 'removeFromFavoriteList',
63
- },
64
54
  },
65
55
  },
66
56
  loading: {
@@ -105,72 +95,90 @@ export default function (request) {
105
95
  },
106
96
  },
107
97
  },
108
- adding: {
109
- invoke: {
110
- src: 'addFavorite',
111
- onDone: {
112
- target: 'idle',
113
- },
114
- onError: {
115
- target: 'idle',
116
- actions: 'removeFromFavoriteList',
117
- },
98
+ },
99
+ },
100
+ timer: {
101
+ initial: 'pending',
102
+ states: {
103
+ pending: {
104
+ after: {
105
+ 20000: [
106
+ {
107
+ target: 'updating',
108
+ cond: 'hasRoom',
109
+ },
110
+ {
111
+ target: 'pending',
112
+ },
113
+ ],
118
114
  },
119
115
  },
120
- removing: {
116
+ updating: {
121
117
  invoke: {
122
- src: 'removeFavorite',
118
+ src: 'updatePageContent',
123
119
  onDone: {
124
- target: 'idle',
120
+ target: 'pending',
121
+ actions: 'swapContent',
125
122
  },
126
123
  onError: {
127
- target: 'idle',
128
- actions: 'addToFavoriteList',
124
+ target: 'pending',
129
125
  },
130
126
  },
131
127
  },
132
128
  },
133
129
  },
134
- timer: {
135
- initial: 'favorite',
130
+ oldTimer: {
131
+ initial: 'fetchingGameSHA',
136
132
  states: {
137
- favorite: {
133
+ fetchingGameSHA: {
138
134
  invoke: {
139
- src: 'getFavorite',
135
+ src: 'fetchGameSHA',
140
136
  onDone: {
141
- target: 'pending',
142
- actions: 'setFavoriteList',
137
+ target: 'checking',
138
+ actions: 'setGameSHA',
143
139
  },
144
140
  onError: {
145
141
  target: 'pending',
146
142
  },
147
143
  },
148
144
  },
149
- pending: {
150
- after: {
151
- 20000: [
152
- {
153
- target: 'updating',
154
- cond: 'hasRoom',
155
- },
156
- {
157
- target: 'pending',
158
- },
159
- ],
145
+ checking: {
146
+ always: [
147
+ { target: 'updatingStatus', cond: 'isStatusChanged' },
148
+ { target: 'pending' },
149
+ ],
150
+ },
151
+ updatingStatus: {
152
+ invoke: {
153
+ src: 'fetchStatus',
154
+ onDone: {
155
+ target: 'updatingOption',
156
+ actions: ['setStatus'],
157
+ },
158
+ onError: {
159
+ target: 'pending',
160
+ },
160
161
  },
161
162
  },
162
- updating: {
163
+ updatingOption: {
163
164
  invoke: {
164
- src: 'updatePageContent',
165
+ src: 'fetchOption',
165
166
  onDone: {
166
167
  target: 'pending',
167
- actions: 'swapContent',
168
+ actions: 'mergeOption',
168
169
  },
169
170
  onError: {
170
171
  target: 'pending',
171
172
  },
172
173
  },
173
174
  },
175
+ pending: {
176
+ after: {
177
+ 20000: {
178
+ target: 'fetchingGameSHA',
179
+ },
180
+ },
181
+ },
174
182
  },
175
183
  },
176
184
  },
@@ -200,21 +208,23 @@ export default function (request) {
200
208
  }),
201
209
  appendContent: assign((context, event) => {
202
210
  const { room, favoriteList } = context
203
- const { data, ...rest } = event.data
204
- return {
205
- favoriteList: room.categoryId === MY_FAVORITE ? data : favoriteList,
206
- content: {
207
- ...context.content,
208
- ...rest,
209
- data: context.content.data.concat(data),
210
- },
211
+ if (room.categoryId === GAME_SEARCH) {
212
+ return
213
+ } else {
214
+ const { data, ...rest } = event.data
215
+ return {
216
+ content: {
217
+ ...context.content,
218
+ ...rest,
219
+ data: context.content.data.concat(data),
220
+ },
221
+ }
211
222
  }
212
223
  }),
213
224
  swapContent: assign((context, event) => {
214
- const { room, favoriteList } = context
215
- if (event.data.data.length) {
225
+ const item = event.data
226
+ if (item.data?.length) {
216
227
  return {
217
- favoriteList: room.categoryId === MY_FAVORITE ? event.data.data : favoriteList,
218
228
  content: event.data,
219
229
  }
220
230
  }
@@ -240,40 +250,27 @@ export default function (request) {
240
250
  const lang = event.data.locale.replace('-', '_')
241
251
  return { lang }
242
252
  }),
243
- addToFavoriteList: assign((context, event) => {
244
- return {
245
- favoriteList: [...context.favoriteList, event.data],
246
- }
247
- }),
248
- removeFromFavoriteList: assign((context, event) => {
249
- return {
250
- favoriteList: context.favoriteList.filter((item) => item.gameCode !== event.data.gameCode),
251
- }
252
- }),
253
+ ...oldActions
253
254
  },
254
255
  guards: {
255
256
  isDifferentRoom: (context, event) => context.room?.categoryId !== event.data?.categoryId,
256
257
  hasNext: (context, event) => context.index < context?.content?.totalPage - 1,
257
258
  hasRoom: (context, event) => context.room?.categoryId,
258
- isFavorite: (context, event) => context.favoriteList.some((item) => item.gameCode === event.data.gameCode),
259
- isNotFavorite: (context, event) => !context.favoriteList.some((item) => item.gameCode === event.data.gameCode),
259
+ ...oldGuards
260
260
  },
261
261
  services: {
262
- getFavorite: async (context, event) => {
263
- return await request.send(api.getListMyFavorite())
264
- },
265
262
  getPageContent: async (context, event) => {
266
263
  const { room, index } = context
267
- if (room.categoryId === MY_FAVORITE) {
268
- return await request.send(api.getListMyFavorite())
264
+ if (room.categoryId === GAME_SEARCH) {
265
+ return
269
266
  } else {
270
267
  return await request.send(api.getListCategory({ categoryId: room.categoryId, index }))
271
268
  }
272
269
  },
273
270
  updatePageContent: async (context, event) => {
274
271
  const { room, content, index } = context
275
- if (room.categoryId === MY_FAVORITE) {
276
- // return await request.send(api.getListMyFavorite({ timestamp: content.timestamp }))
272
+ if (room.categoryId === GAME_SEARCH) {
273
+ return
277
274
  } else {
278
275
  return await request.send(
279
276
  api.getListCategoryByRange({
@@ -305,23 +302,55 @@ export default function (request) {
305
302
  const provider_id = context.game.providerId
306
303
  return await request.send(api.logoutGame({ uid_site, provider_id }))
307
304
  },
308
- addFavorite: async (context, event) => {
309
- try {
310
- await request.send(api.addListFavorite(event.data.gameCode))
311
- return event.data
312
- } catch (error) {
313
- throw event.data
314
- }
315
- },
316
- removeFavorite: async (context, event) => {
317
- try {
318
- await request.send(api.removeListFavorite(event.data.gameCode))
319
- return event.data
320
- } catch (error) {
321
- throw event.data
322
- }
323
- },
305
+ ...oldServices(request)
324
306
  },
325
307
  }
326
308
  )
327
309
  }
310
+
311
+ const oldContext = {
312
+ sha: {
313
+ status: null,
314
+ },
315
+ isStatusChanged: false,
316
+ statusMap: {},
317
+ optionMap: {},
318
+ }
319
+
320
+ const oldActions = {
321
+ setGameSHA: assign((context, event) => {
322
+ const { sha } = context
323
+ const newSha = event.data
324
+ return {
325
+ sha: newSha,
326
+ isStatusChanged: !sha.status || sha.status !== newSha.status,
327
+ }
328
+ }),
329
+ setStatus: assign((context, event) => {
330
+ return {
331
+ statusMap: event.data,
332
+ }
333
+ }),
334
+ mergeOption: assign((context, event) => {
335
+ const { optionMap } = context
336
+ return {
337
+ optionMap: { ...optionMap, ...event.data },
338
+ }
339
+ }),
340
+ }
341
+
342
+ const oldGuards = {
343
+ isStatusChanged: (context, event) => context.isStatusChanged,
344
+ }
345
+
346
+ const oldServices = (request) => ({
347
+ fetchOption: async (context, event) => {
348
+ return await request.send(api.getOptionMap())
349
+ },
350
+ fetchGameSHA: async (context, event) => {
351
+ return await request.send(api.getGameSHA())
352
+ },
353
+ fetchStatus: async (context, event) => {
354
+ return await request.send(api.getGameStatus())
355
+ },
356
+ })
@@ -7,6 +7,7 @@ import error from './error.js'
7
7
  import game from './game.js'
8
8
  import game_v2 from './game_v2.js'
9
9
  import game_favorite from './game_favorite.js'
10
+ import game_favorite_v2 from './game_favorite_v2.js'
10
11
  import game_search from './game_search.js'
11
12
  import game_hot_new from './game_hot_new.js'
12
13
  import inbox from './inbox.js'
@@ -42,6 +43,7 @@ export default {
42
43
  game,
43
44
  game_v2,
44
45
  game_favorite,
46
+ game_favorite_v2,
45
47
  game_search,
46
48
  game_hot_new,
47
49
  inbox,
@@ -73,8 +73,11 @@ export default function (request) {
73
73
  return { menu }
74
74
  }),
75
75
  setDefaultMenuItemStack: assign((context, event) => {
76
- const menuItemStack = getDefaultItem(event.data.data[0])
77
- return { menuItemStack }
76
+ const menu = event.data
77
+ if (menu.data.length > 0) {
78
+ const menuItemStack = getDefaultItem(menu.data[0])
79
+ return { menuItemStack }
80
+ }
78
81
  }),
79
82
  setMenuItemStack: assign((context, event) => {
80
83
  const { menuItemStack } = context
@@ -1 +1,2 @@
1
- export const MY_FAVORITE = 'MyFavorite'
1
+ export const MY_FAVORITE = 'MyFavorite'
2
+ export const GAME_SEARCH = 'GameSearch'