jason-rails 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -0
  3. data/Gemfile.lock +152 -2
  4. data/app/controllers/jason/api_controller.rb +1 -1
  5. data/client/lib/JasonContext.d.ts +6 -1
  6. data/client/lib/JasonProvider.d.ts +2 -2
  7. data/client/lib/JasonProvider.js +5 -124
  8. data/client/lib/createJasonReducers.js +41 -3
  9. data/client/lib/createOptDis.js +0 -2
  10. data/client/lib/createPayloadHandler.d.ts +6 -1
  11. data/client/lib/createPayloadHandler.js +42 -54
  12. data/client/lib/createServerActionQueue.d.ts +10 -0
  13. data/client/lib/createServerActionQueue.js +48 -0
  14. data/client/lib/createServerActionQueue.test.d.ts +1 -0
  15. data/client/lib/createServerActionQueue.test.js +37 -0
  16. data/client/lib/index.d.ts +3 -2
  17. data/client/lib/pruneIdsMiddleware.d.ts +2 -0
  18. data/client/lib/pruneIdsMiddleware.js +26 -0
  19. data/client/lib/restClient.d.ts +2 -0
  20. data/client/lib/restClient.js +17 -0
  21. data/client/lib/useJason.d.ts +5 -0
  22. data/client/lib/useJason.js +99 -0
  23. data/client/lib/useJason.test.d.ts +1 -0
  24. data/client/lib/useJason.test.js +79 -0
  25. data/client/lib/useSub.js +1 -0
  26. data/client/package.json +4 -3
  27. data/client/src/JasonProvider.tsx +5 -123
  28. data/client/src/createJasonReducers.ts +49 -3
  29. data/client/src/createOptDis.ts +0 -2
  30. data/client/src/createPayloadHandler.ts +47 -63
  31. data/client/src/createServerActionQueue.test.ts +42 -0
  32. data/client/src/createServerActionQueue.ts +47 -0
  33. data/client/src/pruneIdsMiddleware.ts +24 -0
  34. data/client/src/restClient.ts +13 -0
  35. data/client/src/useJason.test.ts +81 -0
  36. data/client/src/useJason.ts +115 -0
  37. data/client/src/useSub.ts +1 -0
  38. data/client/yarn.lock +59 -3
  39. data/jason-rails.gemspec +4 -0
  40. data/lib/jason.rb +12 -0
  41. data/lib/jason/api_model.rb +2 -12
  42. data/lib/jason/channel.rb +43 -21
  43. data/lib/jason/lua_generator.rb +49 -0
  44. data/lib/jason/publisher.rb +76 -35
  45. data/lib/jason/publisher_old.rb +112 -0
  46. data/lib/jason/subscription.rb +322 -99
  47. data/lib/jason/subscription_old.rb +171 -0
  48. data/lib/jason/version.rb +1 -1
  49. metadata +67 -3
@@ -2,8 +2,8 @@ import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
2
2
  import pluralize from 'pluralize'
3
3
  import _ from 'lodash'
4
4
 
5
- function generateSlices(schema) {
6
- const sliceNames = schema.map(k => pluralize(k))
5
+ function generateSlices(models) {
6
+ const sliceNames = models.map(k => pluralize(k))
7
7
  const adapter = createEntityAdapter()
8
8
 
9
9
  return _.fromPairs(_.map(sliceNames, name => {
@@ -30,6 +30,52 @@ function generateSlices(schema) {
30
30
  }))
31
31
  }
32
32
 
33
+ function generateJasonSlices(models) {
34
+ const initialState = _.fromPairs(_.map(models, (model_name) => {
35
+ return [model_name, {}]
36
+ }))
37
+
38
+ const modelSliceReducer = createSlice({
39
+ name: 'jasonModels',
40
+ initialState,
41
+ reducers: {
42
+ setSubscriptionIds(s,a) {
43
+ const { payload } = a
44
+ const { subscriptionId, model, ids } = payload
45
+ s[model][subscriptionId] = ids
46
+ },
47
+ addSubscriptionId(s,a) {
48
+ const { payload } = a
49
+ const { subscriptionId, model, id } = payload
50
+ s[model][subscriptionId] = _.union(s[model][subscriptionId] || [], [id])
51
+ },
52
+ removeSubscriptionId(s,a) {
53
+ const { payload } = a
54
+ const { subscriptionId, model, id } = payload
55
+ s[model][subscriptionId] = _.remove(s[model][subscriptionId] || [], id)
56
+ }
57
+ }
58
+ }).reducer
59
+
60
+ const jasonSliceReducer = createSlice({
61
+ name: 'jason',
62
+ initialState: {
63
+ connected: false,
64
+ queueSize: 0
65
+ },
66
+ reducers: {
67
+ upsert: (s,a) => ({ ...s, ...a.payload })
68
+ }
69
+ }).reducer
70
+
71
+ return { jason: jasonSliceReducer, jasonModels: modelSliceReducer }
72
+ }
73
+
33
74
  export default function createJasonReducers(schema) {
34
- return generateSlices(_.keys(schema))
75
+ const models = _.keys(schema)
76
+
77
+ return {
78
+ ...generateSlices(models),
79
+ ...generateJasonSlices(models)
80
+ }
35
81
  }
@@ -13,7 +13,6 @@ function enrich(type, payload) {
13
13
 
14
14
  export default function createOptDis(schema, dispatch, restClient, serverActionQueue) {
15
15
  const plurals = _.keys(schema).map(k => pluralize(k))
16
- let inFlight = false
17
16
 
18
17
  function enqueueServerAction (action) {
19
18
  serverActionQueue.addItem(action)
@@ -23,7 +22,6 @@ export default function createOptDis(schema, dispatch, restClient, serverActionQ
23
22
  const action = serverActionQueue.getItem()
24
23
  if (!action) return
25
24
 
26
- inFlight = true
27
25
  restClient.post('/jason/api/action', action)
28
26
  .then(serverActionQueue.itemProcessed)
29
27
  .catch(e => {
@@ -2,18 +2,17 @@ import { apply_patch } from 'jsonpatch'
2
2
  import deepCamelizeKeys from './deepCamelizeKeys'
3
3
  import pluralize from 'pluralize'
4
4
  import _ from 'lodash'
5
- import { validate as isUuid } from 'uuid'
5
+ import { validate as isUuid, v4 as uuidv4 } from 'uuid'
6
6
 
7
7
  function diffSeconds(dt2, dt1) {
8
8
  var diff =(dt2.getTime() - dt1.getTime()) / 1000
9
9
  return Math.abs(Math.round(diff))
10
10
  }
11
11
 
12
- export default function createPayloadHandler(dispatch, serverActionQueue, subscription, model, config) {
13
- console.log({ model, config })
14
- let payload = [] as any[]
15
- let previousPayload = [] as any[]
16
- let idx = 0
12
+ export default function createPayloadHandler({ dispatch, serverActionQueue, subscription, config }) {
13
+ const subscriptionId = uuidv4()
14
+
15
+ let idx = {}
17
16
  let patchQueue = {}
18
17
 
19
18
  let lastCheckAt = new Date()
@@ -21,8 +20,7 @@ export default function createPayloadHandler(dispatch, serverActionQueue, subscr
21
20
  let checkInterval
22
21
 
23
22
  function getPayload() {
24
- console.log({ getPayload: model, subscription })
25
- subscription.send({ getPayload: { model, config } })
23
+ setTimeout(() => subscription.send({ getPayload: config }), 1000)
26
24
  }
27
25
 
28
26
  function camelizeKeys(item) {
@@ -31,87 +29,73 @@ export default function createPayloadHandler(dispatch, serverActionQueue, subscr
31
29
 
32
30
  const tGetPayload = _.throttle(getPayload, 10000)
33
31
 
34
- function dispatchPayload() {
35
- // We want to avoid updates from server overwriting changes to local state, so if there is a queue then wait.
36
- if (!serverActionQueue.fullySynced()) {
37
- console.log(serverActionQueue.getData())
38
- setTimeout(dispatchPayload, 100)
39
- return
40
- }
41
-
42
- const includeModels = (config.includeModels || []).map(m => _.camelCase(m))
43
-
44
- console.log("Dispatching", { payload, includeModels })
45
-
46
- includeModels.forEach(m => {
47
- const subPayload = _.flatten(_.compact(camelizeKeys(payload).map(instance => instance[m])))
48
- const previousSubPayload = _.flatten(_.compact(camelizeKeys(previousPayload).map(instance => instance[m])))
49
-
50
- // Find IDs that were in the payload but are no longer
51
- const idsToRemove = _.difference(previousSubPayload.map(i => i.id), subPayload.map(i => i.id))
52
-
53
- dispatch({ type: `${pluralize(m)}/upsertMany`, payload: subPayload })
54
- dispatch({ type: `${pluralize(m)}/removeMany`, payload: idsToRemove })
55
- })
56
-
57
- const idsToRemove = _.difference(previousPayload.map(i => i.id), payload.map(i => i.id))
58
-
59
- dispatch({ type: `${pluralize(model)}/upsertMany`, payload: camelizeKeys(payload) })
60
- dispatch({ type: `${pluralize(model)}/removeMany`, payload: idsToRemove })
61
- previousPayload = payload
62
- }
63
-
64
- function processQueue() {
65
- console.log({ idx, patchQueue })
32
+ function processQueue(model) {
33
+ console.debug("processQueue", model, idx[model], patchQueue[model])
66
34
  lastCheckAt = new Date()
67
- if (patchQueue[idx]) {
68
- payload = apply_patch(payload, patchQueue[idx])
69
- if (patchQueue[idx]) {
70
- dispatchPayload()
35
+ if (patchQueue[model][idx[model]]) {
36
+ if (!serverActionQueue.fullySynced()) {
37
+ console.debug(serverActionQueue.getData())
38
+ setTimeout(() => processQueue(model), 100)
39
+ return
71
40
  }
72
- delete patchQueue[idx]
73
- idx++
41
+
42
+ const { payload, destroy, id, type } = patchQueue[model][idx[model]]
43
+
44
+ if (type === 'payload') {
45
+ dispatch({ type: `${pluralize(model)}/upsertMany`, payload })
46
+ const ids = payload.map(instance => instance.id)
47
+ dispatch({ type: `jasonModels/setSubscriptionIds`, payload: { model, subscriptionId, ids }})
48
+ } else if (destroy) {
49
+ dispatch({ type: `${pluralize(model)}/remove`, payload: id })
50
+ dispatch({ type: `jasonModels/removeSubscriptionId`, payload: { model, subscriptionId, id }})
51
+ } else {
52
+ dispatch({ type: `${pluralize(model)}/upsert`, payload })
53
+ dispatch({ type: `jasonModels/addSubscriptionId`, payload: { model, subscriptionId, id }})
54
+ }
55
+
56
+ delete patchQueue[model][idx[model]]
57
+ idx[model]++
74
58
  updateDeadline = null
75
- processQueue()
59
+ processQueue(model)
76
60
  // If there are updates in the queue that are ahead of the index, some have arrived out of order
77
61
  // Set a deadline for new updates before it declares the update missing and refetches.
78
- } else if (_.keys(patchQueue).length > 0 && !updateDeadline) {
62
+ } else if (_.keys(patchQueue[model]).length > 0 && !updateDeadline) {
79
63
  var t = new Date()
80
64
  t.setSeconds(t.getSeconds() + 3)
81
65
  updateDeadline = t
82
- setTimeout(processQueue, 3100)
66
+ setTimeout(() => processQueue(model), 3100)
83
67
  // If more than 10 updates in queue, or deadline has passed, restart
84
- } else if (_.keys(patchQueue).length > 10 || (updateDeadline && diffSeconds(updateDeadline, new Date()) < 0)) {
68
+ } else if (_.keys(patchQueue[model]).length > 10 || (updateDeadline && diffSeconds(updateDeadline, new Date()) < 0)) {
85
69
  tGetPayload()
86
70
  updateDeadline = null
87
71
  }
88
72
  }
89
73
 
90
74
  function handlePayload(data) {
91
- const { value, idx: newIdx, diff, latency, type } = data
92
- console.log({ data })
75
+ const { idx: newIdx, model: snake_model, type } = data
76
+ const model = _.camelCase(snake_model)
93
77
 
94
- if (type === 'payload') {
95
- if (!value) return null;
78
+ idx[model] = idx[model] || 0
79
+ patchQueue[model] = patchQueue[model] || {}
96
80
 
97
- payload = value
98
- dispatchPayload()
99
- idx = newIdx + 1
81
+ if (type === 'payload') {
82
+ idx[model] = newIdx
100
83
  // Clear any old changes left in the queue
101
- patchQueue= _.pick(patchQueue, _.keys(patchQueue).filter(k => k > newIdx + 1))
102
- return
84
+ patchQueue[model] = _.pick(patchQueue[model], _.keys(patchQueue[model]).filter(k => k > newIdx + 1))
103
85
  }
104
86
 
105
- patchQueue[newIdx] = diff
106
-
107
- processQueue()
87
+ patchQueue[model][newIdx] = camelizeKeys({ ...data, model })
88
+ console.debug("Added to queue", model, idx[model], camelizeKeys({ ...data, model }), serverActionQueue.getData())
89
+ processQueue(model)
108
90
 
109
91
  if (diffSeconds((new Date()), lastCheckAt) >= 3) {
110
92
  lastCheckAt = new Date()
111
- console.log('Interval lost. Pulling from server')
93
+ console.debug('Interval lost. Pulling from server')
112
94
  tGetPayload()
113
95
  }
114
96
  }
115
97
 
98
+ tGetPayload()
99
+
116
100
  return handlePayload
117
101
  }
@@ -0,0 +1,42 @@
1
+ import createServerActionQueue from './createServerActionQueue'
2
+
3
+ test('Adding items', () => {
4
+ const serverActionQueue = createServerActionQueue()
5
+ serverActionQueue.addItem({ type: 'entity/add', payload: { id: 'abc', attribute: 1 } })
6
+ const item = serverActionQueue.getItem()
7
+ expect(item).toStrictEqual({ type: 'entity/add', payload: { id: 'abc', attribute: 1 } })
8
+ })
9
+
10
+ test('Deduping of items that will overwrite each other', () => {
11
+ const serverActionQueue = createServerActionQueue()
12
+ serverActionQueue.addItem({ type: 'entity/upsert', payload: { id: 'abc', attribute: 1 } })
13
+ serverActionQueue.addItem({ type: 'entity/upsert', payload: { id: 'abc', attribute: 2 } })
14
+ serverActionQueue.addItem({ type: 'entity/upsert', payload: { id: 'abc', attribute: 3 } })
15
+
16
+ const item = serverActionQueue.getItem()
17
+
18
+ expect(item).toStrictEqual({ type: 'entity/upsert', payload: { id: 'abc', attribute: 3 } })
19
+ })
20
+
21
+ test('Deduping of items with a superset', () => {
22
+ const serverActionQueue = createServerActionQueue()
23
+ serverActionQueue.addItem({ type: 'entity/upsert', payload: { id: 'abc', attribute: 1 } })
24
+ serverActionQueue.addItem({ type: 'entity/upsert', payload: { id: 'abc', attribute: 2, attribute2: 'test' } })
25
+
26
+ const item = serverActionQueue.getItem()
27
+
28
+ expect(item).toStrictEqual({ type: 'entity/upsert', payload: { id: 'abc', attribute: 2, attribute2: 'test' } })
29
+ })
30
+
31
+ test("doesn't dedupe items with some attributes missing", () => {
32
+ const serverActionQueue = createServerActionQueue()
33
+ serverActionQueue.addItem({ type: 'entity/upsert', payload: { id: 'abc', attribute: 1 } })
34
+ serverActionQueue.addItem({ type: 'entity/upsert', payload: { id: 'abc', attribute2: 'test' } })
35
+
36
+ const item = serverActionQueue.getItem()
37
+ serverActionQueue.itemProcessed()
38
+ const item2 = serverActionQueue.getItem()
39
+
40
+ expect(item).toStrictEqual({ type: 'entity/upsert', payload: { id: 'abc', attribute: 1 } })
41
+ expect(item2).toStrictEqual({ type: 'entity/upsert', payload: { id: 'abc', attribute2: 'test' } })
42
+ })
@@ -0,0 +1,47 @@
1
+ // A FIFO queue with deduping of actions whose effect will be cancelled by later actions
2
+
3
+ import _ from 'lodash'
4
+
5
+ export default function createServerActionQueue() {
6
+ const queue: any[] = []
7
+ let inFlight = false
8
+
9
+ function addItem(item) {
10
+ // Check if there are any items ahead in the queue that this item would effectively overwrite.
11
+ // In that case we can remove them
12
+ // If this is an upsert && item ID is the same && current item attributes are a superset of the earlier item attributes
13
+ const { type, payload } = item
14
+ if (type.split('/')[1] !== 'upsert') {
15
+ queue.push(item)
16
+ return
17
+ }
18
+
19
+ _.remove(queue, item => {
20
+ const { type: itemType, payload: itemPayload } = item
21
+ if (type !== itemType) return false
22
+ if (itemPayload.id !== payload.id) return false
23
+
24
+ // Check that all keys of itemPayload are in payload.
25
+ return _.difference(_.keys(itemPayload),_.keys(payload)).length === 0
26
+ })
27
+
28
+ queue.push(item)
29
+ }
30
+
31
+ return {
32
+ addItem,
33
+ getItem: () => {
34
+ if (inFlight) return false
35
+
36
+ const item = queue.shift()
37
+ if (item) {
38
+ inFlight = true
39
+ return item
40
+ }
41
+ return false
42
+ },
43
+ itemProcessed: () => inFlight = false,
44
+ fullySynced: () => queue.length === 0 && !inFlight,
45
+ getData: () => ({ queue, inFlight })
46
+ }
47
+ }
@@ -0,0 +1,24 @@
1
+ import _ from 'lodash'
2
+ import pluralize from 'pluralize'
3
+
4
+ const pruneIdsMiddleware = schema => store => next => action => {
5
+ const { type } = action
6
+ const result = next(action)
7
+ const state = store.getState()
8
+ if (type === 'jasonModels/setSubscriptionIds') {
9
+ // Check every model
10
+ _.map(_.keys(schema), model => {
11
+ let ids = []
12
+ _.map(state.jasonModels[model], (subscribedIds, k) => {
13
+ ids = _.union(ids, subscribedIds)
14
+ })
15
+ // Find IDs currently in Redux that aren't in any subscription
16
+ const idsToRemove = _.difference(state[pluralize(model)].ids, ids)
17
+ store.dispatch({ type: `${pluralize(model)}/removeMany`, payload: idsToRemove })
18
+ })
19
+ }
20
+
21
+ return result
22
+ }
23
+
24
+ export default pruneIdsMiddleware
@@ -0,0 +1,13 @@
1
+ import axios from 'axios'
2
+ import applyCaseMiddleware from 'axios-case-converter'
3
+ import { validate as isUuid } from 'uuid'
4
+
5
+ const csrfToken = (document?.querySelector("meta[name=csrf-token]") as any)?.content
6
+ axios.defaults.headers.common['X-CSRF-Token'] = csrfToken
7
+ const restClient = applyCaseMiddleware(axios.create() as any, {
8
+ preservedKeys: (key) => {
9
+ return isUuid(key)
10
+ }
11
+ })
12
+
13
+ export default restClient
@@ -0,0 +1,81 @@
1
+ import { renderHook, act } from '@testing-library/react-hooks'
2
+ import useJason from './useJason'
3
+ import restClient from './restClient'
4
+
5
+ jest.mock('./restClient')
6
+
7
+ test('it works', async () => {
8
+ const resp = { data: { post: {} } };
9
+ // @ts-ignore
10
+ restClient.get.mockResolvedValue(resp);
11
+
12
+ const { result, waitForNextUpdate } = renderHook(() => useJason({ reducers: {
13
+ test: (s,a) => s || {}
14
+ }}));
15
+
16
+ await waitForNextUpdate()
17
+ const [store, value, connected] = result.current
18
+ const { handlePayload, subscribe } = value
19
+
20
+ const subscription = subscribe({ post: {} })
21
+
22
+ handlePayload({
23
+ type: 'payload',
24
+ model: 'post',
25
+ payload: [{ id: 4, name: 'test' }],
26
+ md5Hash: subscription.md5Hash,
27
+ idx: 1
28
+ })
29
+
30
+ handlePayload({
31
+ id: 4,
32
+ model: 'post',
33
+ destroy: true,
34
+ md5Hash: subscription.md5Hash,
35
+ idx: 2
36
+ })
37
+
38
+ handlePayload({
39
+ id: 5,
40
+ model: 'post',
41
+ payload: { id: 5, name: 'test2' },
42
+ md5Hash: subscription.md5Hash,
43
+ idx: 3
44
+ })
45
+ })
46
+
47
+ test('pruning IDs', async () => {
48
+ const resp = { data: { post: {} } };
49
+
50
+ // @ts-ignore
51
+ restClient.get.mockResolvedValue(resp);
52
+
53
+ const { result, waitForNextUpdate } = renderHook(() => useJason({ reducers: {
54
+ test: (s,a) => s || {}
55
+ }}));
56
+
57
+ await waitForNextUpdate()
58
+ const [store, value, connected] = result.current
59
+ const { handlePayload, subscribe } = value
60
+
61
+ const subscription = subscribe({ post: {} })
62
+
63
+ handlePayload({
64
+ type: 'payload',
65
+ model: 'post',
66
+ payload: [{ id: 4, name: 'test' }],
67
+ md5Hash: subscription.md5Hash,
68
+ idx: 1
69
+ })
70
+
71
+ handlePayload({
72
+ type: 'payload',
73
+ model: 'post',
74
+ payload: [{ id: 5, name: 'test it out' }],
75
+ md5Hash: subscription.md5Hash,
76
+ idx: 2
77
+ })
78
+
79
+ // The ID 4 should have been pruned
80
+ expect(store.getState().posts.ids).toStrictEqual([5])
81
+ })