Last active
March 31, 2017 20:28
-
-
Save feliperohdee/0173152af16b237862c07b4e7c2afdde to your computer and use it in GitHub Desktop.
Usage of smallorange-reactive-store with new typedAttributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const { | |
Model, | |
Collection, | |
rxjs, | |
lodash | |
} = one; | |
class MessageMeta extends Model { | |
constructor() { | |
const now = Date.now(); | |
super(null, null, { | |
defaults: { | |
id: 'meta', | |
createdAt: now, | |
updatedAt: now, | |
read: false | |
} | |
}); | |
} | |
read() { | |
this.set({ | |
read: true, | |
updatedAt: Date.now() | |
}); | |
} | |
} | |
class Message extends Model { | |
constructor(attributes, options) { | |
super(attributes, options, { | |
defaults: { | |
id: Date.now(), | |
meta: {}, | |
text: '' | |
}, | |
typedAttributes: { | |
meta: MessageMeta | |
} | |
}); | |
} | |
} | |
class Messages extends Collection { | |
constructor() { | |
super(Message, null, null, { | |
defaultOrder: { | |
id: 'asc' | |
} | |
}); | |
} | |
} | |
class Conversation extends Model { | |
constructor(attributes) { | |
super(attributes, null, { | |
typedAttributes: { | |
messages: Messages | |
} | |
}); | |
const { | |
body | |
} = document; | |
const pre = document.createElement('pre'); | |
this.on('set') | |
.map(() => this.toJS()) | |
.subscribe(response => { | |
pre.innerHTML = `\n\n${new Date()}\n${JSON.stringify(response, null, 2)}`; | |
const messages = this.get('messages'); | |
if(messages.size() > 10){ | |
messages | |
.keepLast(5); | |
} | |
body.appendChild(pre); | |
}); | |
} | |
} | |
const conversation = window.conversation = new Conversation({ | |
messages: { | |
text: 'Text I' | |
} | |
}); | |
let index = 0; | |
setInterval(() => { | |
conversation | |
.get('messages') | |
.set({ | |
text: `text ${index++}` | |
}); | |
setTimeout(() => { | |
conversation | |
.get('messages') | |
.last() | |
.get('meta') | |
.read(); | |
}, 700); | |
}, 1 * 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment