const nest = require('depnest') const { computed, watch, map, Struct } = require('mutant') exports.gives = nest('channel.async.suggest') exports.needs = nest({ 'channel.obs.recent': 'first', 'channel.obs.subscribed': 'first', 'keys.sync.id': 'first', }) exports.create = function (api) { var suggestions = null var subscribed = null return nest('channel.async.suggest', function () { loadSuggestions() return function (word) { if (!word) { return suggestions().slice(0, 100) } else { return suggestions().filter((item) => { return item.title.toLowerCase().startsWith(word.toLowerCase()) }) } } }) function loadSuggestions () { if (!suggestions) { var id = api.keys.sync.id() subscribed = api.channel.obs.subscribed(id) var recentlyUpdated = api.channel.obs.recent() var contacts = computed([subscribed, recentlyUpdated], function (a, b) { var result = Array.from(a) b.forEach((item, i) => { if (!result.includes(item)) { result.push(item) } }) return result }) suggestions = map(contacts, suggestion, {idle: true}) watch(suggestions) } } function suggestion (id) { return Struct({ title: id, id: `#${id}`, subtitle: computed([id, subscribed], subscribedCaption), value: computed([id], mention) }) } } function subscribedCaption (id, subscribed) { if (subscribed.has(id)) { return 'subscribed' } } function mention (id) { return `[#${id}](#${id})` }